package com.example.simpletodolist;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class ToDoItemView extends Activity {
//	private ToDoListDatabase mDbHelper;
	static final int MENU_ADD = 0;
	static final int MENU_EDIT = 1;
	static final int LISTVIEW_VIEW = 3;

	private String title;
	private String body;
	private String date;
	private String time;
	private long rowId;
		
	private TextView titleTextView, bodyTextView, dateTextView, timeTextView;
	//private TextView itemTextView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_view);
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
	
		findViews();
		
		//TODO temporary logs to see what value of ROWID is
		Bundle b;
		if (savedInstanceState !=null)
		{
		b= savedInstanceState;
		Long tmprowid = b.getLong(ToDoListDatabase.KEY_ROWID);
		String tmptitle = b.getString(ToDoListDatabase.COL_TITLE);
		String tmpbody = b.getString(ToDoListDatabase.COL_BODY);
		String tmptime = b.getString(ToDoListDatabase.COL_TIME);
		String tmpdate = b.getString(ToDoListDatabase.COL_DATE);
		logSavedState(tmprowid, tmptitle, tmpbody, tmptime, tmpdate);
		}
		///////////////////////////////////////////////////
		//TODO temporary logs to see what value of ROWID is
		b = bundle;
		Long tmp2rowid = b.getLong(ToDoListDatabase.KEY_ROWID);
		String tmp2title = b.getString(ToDoListDatabase.COL_TITLE);
		String tmp2body = b.getString(ToDoListDatabase.COL_BODY);
		String tmp2time = b.getString(ToDoListDatabase.COL_TIME);
		String tmp2date = b.getString(ToDoListDatabase.COL_DATE);
		logViewActivityBundle(tmp2rowid, tmp2title, tmp2body, tmp2time,
				tmp2date);
		///////////////////////////////////////////////////

		if (bundle != null)
			setTextForTextViews(bundle);
		
		// setEventHandlers();
	}

	private void logViewActivityBundle(Long tmp2rowid, String tmp2title,
			String tmp2body, String tmp2time, String tmp2date) {
		Log.d("view activity bundle",tmp2rowid + " rowid");
		Log.d("view activity bundle",tmp2title + " title");
		Log.d("view activity bundle",tmp2body + " body");
		Log.d("view activity bundle",tmp2date + " date");
		Log.d("view activity bundle",tmp2time + " time");
	}

	private void logSavedState(Long tmprowid, String tmptitle, String tmpbody,
			String tmptime, String tmpdate) {
		Log.d("view savedState bundle",tmprowid + " rowid");
		Log.d("view savedState bundle",tmptitle + " title");
		Log.d("view savedState bundle",tmpbody + " body");
		Log.d("view savedState bundle",tmpdate + " date");
		Log.d("view savedState bundle",tmptime + " time");
	}

	private void findViews() {
		titleTextView = (TextView)findViewById(R.id.titleItemTextView);
		bodyTextView = (TextView)findViewById(R.id.bodyItemTextView);
		dateTextView = (TextView)findViewById(R.id.deadlineItemText);
		timeTextView = (TextView)findViewById(R.id.timeItemText);
	}

	private void setTextForTextViews(Bundle bundle) {
		titleTextView.setText(bundle.getString(ToDoListDatabase.COL_TITLE));
		dateTextView.setText(bundle.getString(ToDoListDatabase.COL_DATE));
		bodyTextView.setText(bundle.getString(ToDoListDatabase.COL_BODY));
		timeTextView.setText(bundle.getString(ToDoListDatabase.COL_TIME));
		rowId = bundle.getLong(ToDoListDatabase.KEY_ROWID);
		
		titleTextView.setTypeface(null, Typeface.BOLD_ITALIC);
		dateTextView.setTypeface(null, Typeface.ITALIC);
	}
		
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // getMenuInflater().inflate(R.menu.activity_simple_to_do_list, menu);
    	getMenuInflater().inflate(R.menu.list_context_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.menu_edit:
        		editItem();
        		return true;
        case R.id.menu_delete:
        		deleteItem(rowId);
        		return true;        		
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    private void editItem() {
		// Intent intent = new Intent(this, ToDoItemView.class);
    	Intent intent = new Intent(this, ToDoItemEdit.class);
		Bundle bundle = new Bundle();
		initializeBundleValues();
		putBundleStringsForEditItem(bundle);
		intent.putExtras(bundle);
		startActivityForResult(intent, MENU_EDIT);
		finish();
    }
    
    private void initializeBundleValues() {
    	this.title = this.titleTextView.getText().toString();
    	this.date = this.dateTextView.getText().toString();
    	this.body = this.bodyTextView.getText().toString();
    	this.time = this.timeTextView.getText().toString();
    }

	private void putBundleStringsForEditItem(Bundle bundle) {
		bundle.putString(ToDoListDatabase.COL_TITLE, title);
		bundle.putString(ToDoListDatabase.COL_BODY, body);
		bundle.putString(ToDoListDatabase.COL_DATE, date);
		bundle.putString(ToDoListDatabase.COL_TIME, time);
		bundle.putLong(ToDoListDatabase.KEY_ROWID, rowId);
	}
    
    private void deleteItem(long rowId) {
    	Uri itemToDelete = Uri.withAppendedPath(ToDoListProvider.CONTENT_URI, String.valueOf(rowId));
    	getContentResolver().delete(itemToDelete, null, null);
    	removeItemFromAlarm(rowId);
    	finish();
    }
    
	private void removeItemFromAlarm(long id) {
		try {
			int rowId = (int) id;	// user would have many to-do's if this cast threw an exception
			Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
			
			PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), rowId, intent, 0);
			AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
			alarmManager.cancel(pendingIntent);
		} catch (Exception e) 
		{
			Log.d("on removeItemFromAlarm", e.getMessage());
		}
	}
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {    		
    		super.onActivityResult(requestCode, resultCode, intent);
    		if (intent != null) {
    			Bundle extras = intent.getExtras();
    			switch(requestCode) {
    			case MENU_EDIT:
    				setTextForResult(extras);
    				break;
    			}
    		}
    }

	private void setTextForResult(Bundle extras) {
		title = extras.getString(ToDoListDatabase.COL_TITLE);
		body = extras.getString(ToDoListDatabase.COL_BODY);
		date = extras.getString(ToDoListDatabase.COL_DATE);
		time = extras.getString(ToDoListDatabase.COL_TIME);
		rowId = extras.getLong(ToDoListDatabase.KEY_ROWID);
		titleTextView.setText(title);
		bodyTextView.setText(body);
		dateTextView.setText(date);
		timeTextView.setText(time);
	}
}
